﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Cartoon.Config;
using Cartoon.Model;
using Cartoon.Logic;
using Belyn.Common.Web;
using System.Text.RegularExpressions;
using System.IO;
using Cartoon.Common;
using System.Windows.Threading;
using Cartoon.Skin;
using System.ComponentModel;

namespace CartoonViewer
{
    /// <summary>
    /// W_CartoonViewer.xaml 的交互逻辑
    /// </summary>
    public partial class W_CartoonViewer: Window
    {

        public W_CartoonViewer()
        {
            InitializeComponent();

            SkinS = new SkinService();
            SkinS.Changed += new Action(SkinS_Changed);
            

           

            initWindow();

        } 

        #region 变量
         
        private CartoonDownloadManage cartoonDownloadMgr; 
        private ISkinService SkinS;

        

        #endregion 

        private void Window_Loaded(object sender, RoutedEventArgs e)
        { 
            Log.log("启动漫漫看...");

            LoadConfig(); 

            CartoonPresenter cartoonPresenter = new CartoonPresenter(this.chapterList);
            this.cartoonDownloadMgr = new CartoonDownloadManage();
            this.PlDownload.DownloadManage = this.cartoonDownloadMgr; 
            this.chapterList.CartoonDownLoadMgr = this.cartoonDownloadMgr;
            this.chapterList.Skin = this.SkinS;

            this.PlCartoonList.ChapterList = this.chapterList;
            this.PlCartoonList.LoadComics();
              
        }

        private void SkinS_Changed()
        {
            this.LoadBackGround();
        }
         
        private void initWindow()
        { 
            this.WindowStyle = WindowStyle.None; 
            this.LoadBackGround(); 
        }
        
		#region 加载界面主题

        private ImageBrush mainBackGround;
        private void LoadBackGround()
        {
            ImageBrush b = new ImageBrush();
            b.ImageSource = this.SkinS.Skin.BackGround;

            mainBackGround = b;
            this.Win.Background = mainBackGround; 
        }

		#endregion
		  
        private ConfigForCartoon configForCartoon; 

        private void LoadConfig()
        {
            Log.log("加载配置...");

            this.configForCartoon = ConfigForCartoon.CreateInstance();
            this.chapterList.CartoonSavePath = this.configForCartoon.SavePath;
            this.chapterList.Domain = this.configForCartoon.Domain;
            this.chapterList.Domain_Pic = this.configForCartoon.PictureDomain;

            this.PlCartoonList.Config = this.configForCartoon;
        }

        private void btn_LoadCartoons_Click(object sender, RoutedEventArgs e)
        {
            this.HideDownloadPanel();
            this.ShowCartoonPanel();
        }

        private void btn_DM_Click(object sender, RoutedEventArgs e)
        {
            this.HideCartoonPanel();
            this.ShowDownloadPanel();
        }

        private void ShowDownloadPanel()
        {
            this.PlDownload.Visibility = Visibility.Visible;
        }

        private void HideDownloadPanel()
        {
            this.PlDownload.Visibility = Visibility.Hidden;
        }

        private void ShowCartoonPanel()
        {
            this.Pl_CartoonList.Visibility = Visibility.Visible;
        }

        private void HideCartoonPanel()
        {
            this.Pl_CartoonList.Visibility = Visibility.Hidden;
        }

        private void btn_Setting_Click(object sender, RoutedEventArgs e)
        {
            this.ShowSetting();
        }

        private void ShowSetting()
        {
            W_Setting wSetting = new W_Setting(this.SkinS);
            wSetting.Saved += new EventHandler(wSetting_Saved);
            wSetting.ShowDialog();
        }

        private void wSetting_Saved(object sender, EventArgs e)
        {
            W_Setting wSetting = sender as W_Setting;
            if (wSetting == null) return;

            this.LoadConfig();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Log.log("停止所有下载...");
            this.cartoonDownloadMgr.StopAll();
            this.chapterList.StopLoadChapterList();
            Log.log("退出漫漫看...");
        } 

		#region 移动窗口
        bool IsWinMoving = false;
        Point pbegin;
        Point pend;
        private void CvsTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        { 
            if (this.PlTitleBtn.IsMouseOver ) return; 

            this.pbegin = e.GetPosition(this);
            this.IsWinMoving = this.CvsTitle.CaptureMouse();
            this.IsWinMoving = true;
            
        }

        private void CvsTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsWinMoving == false) return;

            this.pend = e.GetPosition(this);

            if (pbegin.X == pend.X && pbegin.Y == pend.Y) return;

            double moveX, moveY;

            moveX = pend.X - pbegin.X;
            moveY = pend.Y - pbegin.Y;

            this.Left += moveX;
            this.Top += moveY;

        }

        private void CvsTitle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            
            this.IsWinMoving = false;
            this.CvsTitle.ReleaseMouseCapture();
            
        }

       

      

		#endregion
		
		#region 标题按扭事件 
      
        private void WinBtnClose_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.Close();
        }

        private void WinBtnMin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.WindowState = WindowState.Minimized;
        }  
       
		#endregion 
          
    }
}
